
import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import PlayerData from '../../Script/Common/PlayerData';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import Loader from '../../Script/Common/Loader';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import GlobalPool, { customPoolScript } from '../../Script/Common/GlobalPool';
import Action3dManager, { ActionMngType } from '../../Script/Common/Action3dManager';
const { ccclass, property } = _decorator;

@ccclass('TrySkinUI')
export class TrySkinUI extends yyComponent {

    @property(Node)
    protected carStage: Node = null;
    protected tryCarType: number = 0;
    protected tryCarId: number = 0;

    public show() {
        this.node.active = true;
        let data = PlayerData.getPlayerData().shopData;
        let arr = [];
        let level = PlayerData.getData("gameData.curLevel");
        let maxType = 3;
        if (level == 1) {
            maxType = 2;
        }
        for (let type = 1; type <= maxType; ++type) {
            for (let id = 1; id <= 3; ++id) {
                if (!data[type][id]) {
                    arr.push({
                        type: type,
                        id: id,
                    });
                }
            }
        }
        if (arr.length == 0) {
            setTimeout(() => {
                this.hideUI(GlobalEnum.UI.trySkin);
            }, 0);
            return;
        }
        let index = Math.round(Math.random() * (arr.length - 1));
        this.setTryData(arr[index]);
    }
    protected setTryData(data) {
        this.tryCarType = data.type;
        this.tryCarId = data.id;
        this.showCar(this.tryCarType, this.tryCarId);
    }
    protected showCar(type, id) {
        let str = "ShopCar" + type + "_" + id;
        let node = GlobalPool.get(str);
        if (!!node) {
            this.addCar(node);
        } else {
            Loader.loadBundle("Car", () => {
                Loader.loadBundleRes("Car", "Prefab/ui/" + str, (res) => {
                    if (!res) {
                        return;
                    }
                    GlobalPool.createPool(str, res, "ShopCar");
                    if (this.tryCarType == type && this.tryCarId == id) {
                        let node2 = GlobalPool.get(str);
                        this.addCar(node2);
                    }
                });
            });
        }
    }
    protected addCar(node: Node) {
        GlobalPool.putAllChildren(this.carStage);
        this.carStage.addChild(node);
        node[customPoolScript].show();
    }

    public hide() {
        this.node.active = false;
        this.reset();
        GlobalPool.putAllChildren(this.carStage);
    }

    update(dt) {
        Action3dManager.getMng(ActionMngType.Level).update(dt);
    }

    protected onBtnTry() {
        this.playClickBtn();
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: this.onVideoFinish.bind(this),
        });
    }
    protected onVideoFinish() {
        PlayerData.getPlayerData().useSkin[this.tryCarType] = this.tryCarId;
        PlayerData.getPlayerData().carSkin[this.tryCarType].try = this.tryCarId;
        this.emit(EventType.Common.DirectorEvent.resumeLevel);
        this.hideUI(GlobalEnum.UI.trySkin);
    }

    protected onBtnClose() {
        this.playClickBtn();
        this.emit(EventType.Common.DirectorEvent.resumeLevel);
        this.hideUI(GlobalEnum.UI.trySkin);
    }
}

